//
//  MovieDetailViewController.m
//  UI-豆瓣项目APP
//
//  Created by liuxin on 15/10/27.
//  Copyright © 2015年 孙鹏. All rights reserved.
//

#import "MovieDetailViewController.h"
#import "MovieDetailView.h"
#import "MBProgressHUD.h"
#import "AppTools.h"
#import "CollectMovieDB.h"
#import "LoginViewController.h"
@interface MovieDetailViewController ()
@property(nonatomic,retain)MBProgressHUD *hud;
@property(nonatomic,retain)UIAlertController *alert;
@property(nonatomic,retain)UIAlertController *alertFail;
@end

@implementation MovieDetailViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if ([[NSUserDefaults standardUserDefaults]objectForKey:@"isBtn"]!=nil) {
        [self shareAction];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor greenColor];
    UIImage *image = [UIImage imageNamed:@"btn_nav_share@2x"];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(shareAction)];
    
    UIScrollView *scrollV = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0,375, 667-65)];
    scrollV.backgroundColor = [UIColor whiteColor];
    scrollV.contentSize = CGSizeMake(375, 667*3);
    scrollV.bounces = NO;
    
    MovieDetailView *movieDetailV = [[MovieDetailView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
        movieDetailV.backgroundColor = [UIColor whiteColor];
        [scrollV addSubview:movieDetailV];
        [self.view addSubview:scrollV];
        [movieDetailV release];
    
    self.alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
    self.alertFail = [UIAlertController alertControllerWithTitle:@"提示" message:@"您已经收藏过这部影片了哦" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *ok = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
    }];
    [self.alert addAction:ok];
    [self.alertFail addAction:ok];
    
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.hud show:YES];
    self.hud.labelText = @"正在玩命加载中.....";
    self.hud.labelColor = [UIColor greenColor];
    
    
     NSString *urlStr = [NSString stringWithFormat:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/searchmovie.php?movieId=%@",self.movieID];
    [AppTools getLocalDataWithURL:urlStr andBlock:^(id dic) {
        self.movieDeatilDic = [dic objectForKey:@"result"];
        //        NSLog(@"dict == %@",dict);
        self.navigationItem.title = [self.movieDeatilDic objectForKey:@"title"];
        movieDetailV.movieDICT = [NSMutableDictionary dictionaryWithDictionary:self.movieDeatilDic];
        [movieDetailV getValueHeight:^(CGFloat Height) {
//            NSLog(@"Height == %f",Height);
            scrollV.contentSize =CGSizeMake(375, Height+300);
        }];
        [self.hud hide:YES];
    } ];
}
-(void)shareAction{
    NSLog(@"share  !  !");
    self.btnState = @"yes";
    [[NSUserDefaults standardUserDefaults]setObject:self.btnState forKey:@"isBtn"];
    if( [[NSUserDefaults standardUserDefaults]objectForKey:@"LoginState"]== nil){
        LoginViewController *loginVC = [[LoginViewController alloc]init];
        loginVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:loginVC animated:YES];
    }else{
        NSString *userName = [[NSUserDefaults standardUserDefaults]objectForKey:@"LoginName"];
        NSLog(@"现在登录的用户是:%@",userName);
        [self insertIntoDataBase];
    }
}
-(void)insertIntoDataBase{
    NSString *userName = [[NSUserDefaults standardUserDefaults]objectForKey:@"LoginName"];
    MovieList *shareMovie = [MovieList new];
    shareMovie.movieId = self.movieID;
    shareMovie.movieName = [self.movieDeatilDic objectForKey:@"title"];
    NSArray *array = [[CollectMovieDB shareDataBase]selectMoview:userName];
    NSMutableArray *movieNames = [NSMutableArray array];
    for (MovieList *mo in array) {
        [movieNames addObject:mo.movieName];
    }
    if([movieNames containsObject:shareMovie.movieName] == NO){
        [[CollectMovieDB shareDataBase]insertMovie:shareMovie userName:userName];
        [self presentViewController:self.alert animated:YES completion:^{
        }];
    }else{
        [self presentViewController:self.alertFail animated:YES completion:^{
        }];
    }
    [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"isBtn"];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
